

//////////////////////////////// VIEW OBJECT ///////////////////////////////////////////
// MVC view object for updating the UI on the webpage
/////////////////////////////////////////////////////////////////////////////////////////

// namespace for the MVC view, using the Module Pattern with anonymous constructor
// see http://yuiblog.com/blog/2007/06/12/module-pattern/ and http://www.dustindiaz.com/namespace-your-javascript/
// read the blog comments too
var LCMVC_view = new function() {
	

	// Private inner object 
	function oView() {
		
		// Private properties:	
		// -- none
		// public properties
		// -- none
		// private methods
		// -- none
		
		// privileged methods??
		//
		// Updates the html elements matching the selector
		oView.prototype.UpdateElementsbySelectorWithValue = function(sel, newValue) {
			
			console.log("Updating selector \"" + sel + "\" with value \"" + newValue + "\"" );
			
			// get the node (=tag) type of the element to update and update accordingly
			// the sel is the class, so in practice .each() will run for all the elements of that class.
			// this is useful if I'm showing the same dp on several widgets in the page (textbox, checkbox and image for example)
			$(sel).each(function() {
				if (this.nodeName.toLowerCase() === "select") {
					// if it's a select box:
					// - if value is one of the presets, set it
					// - if it's something else, unhide the hidden OPTION and set that
					//
					// SetSelectWithValue(selectElement, newValue)
					/*
						TODO 
					*/
				
					// array of option elements' value
					var optionValues = [];
					// array of option elements' text
					var optionTexts = [];

				  	// iterate through all option elements
				  	// NOTE: the second $ parameter scopes the first, thereby limiting 
					// 		 the search for "option" only on the "this" object, i.e. this specific <select>
					$("option", this).each(function() {
					    // get value/text and push it into respective array
					    optionValues.push($(this).val());
					    optionTexts.push($(this).text());
				  	});

					// if the new value is not one of the options then only modify the select text
					for (var i in optionValues) {
						if (optionValues.hasOwnProperty(i)) {
							// if it's one of the options, set that
							if (newValue === optionValues[i]) {
								$(this).val(newValue);
								break; // exit for loop
							} 
						}
					}
					// if the value wasn't set in the for loop
					if ($(this).val() !== newValue ) {
							$("option[value=\"Hidden\"]", this).text(newValue).attr('selected', 'selected');
					}
				} else if (this.nodeName.toLowerCase() === "input") {
					// if it's a checkbox
					$(this).attr('checked', (newValue === "ON" || newValue === "100.0 1")?true:false);
				} else {
					console.log("Cannot set value %s for a node <%s>. Element not supported", newValue, this.nodeName.toLowerCase() );
				}
			});
		};

		// return a list of points that need refreshing for whatever reason
		// For now I'm requesting refresh for all the points visible on the current html view
		oView.prototype.GetPointsToRefresh = function() {
		
			// the list of points to refresh
			var list = [];
			
			// for each dp under observation, find those that are actually in the visible page; no point updating them otherwise
			$.each(observedDataPoints_Arr, function (index, value) {
				// get the .current (=visible) DIV and--only within that--get the class that represents the current dp
				$("div.current" + " " + value.dpClass).each( function() {
					// and save the dp path
					list.push(value.dpReadPath);
					console.log("Requesting refresh for " + value.dpReadPath);
					// do this only once for each class found
					return false;
				});
			});
			return list;
		}
	}
	// make public whatever properties/methods we want, and make them accessible via the module name
	return {
		oView: oView, // access it as "var v = new LCMVC_view.oView(...);"
	};
};

